package net.lab1024.sa.base.module.support.rule.service;

import java.time.LocalDateTime;
import java.util.List;

import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.rule.dao.CepDao;
import net.lab1024.sa.base.module.support.rule.domain.entity.CepEntity;
import net.lab1024.sa.base.module.support.rule.domain.entity.RuleCepStep;
import net.lab1024.sa.base.module.support.rule.domain.form.CepAddForm;

import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.module.support.rule.domain.form.CepQueryForm;
import net.lab1024.sa.base.module.support.rule.domain.form.CepUpdateForm;
import net.lab1024.sa.base.module.support.rule.domain.vo.CepVO;
import net.lab1024.sa.base.utils.DateUtilsLocal;
import net.lab1024.sa.base.utils.TextConversionUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 规则主表 Service
 *
 * @Author chenyuqiang
 * @Date 2025-08-21 22:46:37
 * @Copyright 1
 */

@Service
public class CepService {

    @Resource
    private CepDao cepDao;
    @Autowired
    private RuleCepStepService ruleCepStepService;
    /**
     * 分页查询
     */
    public PageResult<CepVO> queryPage(CepQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CepVO> list = cepDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public ResponseDTO<CepEntity> detail(String id) {
        CepEntity cep = cepDao.selectById(id);
        List<RuleCepStep> byRuleId = ruleCepStepService.getByRuleId(cep.getId());
        cep.setRuleCepStepList(byRuleId);
        return ResponseDTO.ok(cep);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(@Valid CepEntity addForm) {
        CepEntity cepEntity = SmartBeanUtil.copy(addForm, CepEntity.class);
        cepEntity.setUpdatedTime(DateUtilsLocal.formatLocalDateTime(LocalDateTime.now()));
        cepEntity.setCreatedTime(DateUtilsLocal.formatLocalDateTime(LocalDateTime.now()));
        if(null==cepEntity.getCreatedBy()){
            cepEntity.setCreatedBy(SmartRequestUtil.getRequestUser().getUserName());
        }
        String epl = cepEntity.getEpl();
        String cnversion = TextConversionUtils.cnversion(epl);
        cepEntity.setEpl(cnversion);

        cepDao.insert(cepEntity);
        List<RuleCepStep> ruleCepStepList = addForm.getRuleCepStepList();
        if(null!=ruleCepStepList && ruleCepStepList.size() > 0){
            ruleCepStepList.forEach(ruleCepStep -> {
                ruleCepStep.setRuleId(cepEntity.getId());
                ruleCepStepService.save(ruleCepStep);
            });
        }
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    @Transactional
    public ResponseDTO<String> update(CepEntity updateForm) {
        CepEntity cepEntity = SmartBeanUtil.copy(updateForm, CepEntity.class);
        cepEntity.setUpdatedTime(DateUtilsLocal.formatLocalDateTime(LocalDateTime.now()));
        if(null==cepEntity.getUpdatedBy()){
        cepEntity.setUpdatedBy(SmartRequestUtil.getRequestUser().getUserName());}
        String epl = cepEntity.getEpl();
        String cnversion = TextConversionUtils.cnversion(epl);
        cepEntity.setEpl(cnversion);
        cepDao.updateById(cepEntity);
        List<RuleCepStep> ruleCepStepList = updateForm.getRuleCepStepList();
        ruleCepStepService.deleteByRuleId(updateForm.getId());
        if(null!=ruleCepStepList && ruleCepStepList.size() > 0){
            ruleCepStepList.forEach(ruleCepStep -> {
                ruleCepStep.setRuleId(cepEntity.getId());
                ruleCepStepService.save(ruleCepStep);
            });
        }
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        cepDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        cepDao.deleteById(id);
        return ResponseDTO.ok();
    }
    public CepEntity detailByCode(String code) {
        return cepDao.detailByCode(code);
    }
}
